#!/bin/bash

# specify the arm as abi, the api level for android kitkat as used by
# google tango and with gnustl_static the c++ support
# for more information look into the android.toolchain.cmake file
export ANDROID_ABI="armeabi armeabi-v7a with NEON"
export ANDROID_NATIVE_API_LEVEL=android-19
export ANDROID_STL=gnustl_static
export ANDROID_STL_FORCE_FEATURES=ON
export ANDROID_NDK=/usr/local/android-ndk-r14b

export CFLAGS="-pipe -w"
export CXXFLAGS=${CFLAGS}

# [[ -z "${jobs}" ]] && jobs=$(grep -cP '^processor' /proc/cpuinfo)
jobs=4

# echo -e "\n\n\033[1;32mCompiling with ${jobs} jobs ...\033[m"

# check that ANDROID_NDK points to a android ndk folder

cd build

ROOT=${PWD}
ANDROIDTOOLCHAIN=${ROOT}/../android.toolchain.cmake

EIGEN_INCLUDE_DIR=${ROOT}/eigen # eigen needs no cross-compiling
FLANN_ROOT=${ROOT}/flann
BOOST_ROOT=${ROOT}/boost
QHULL_ROOT=${ROOT}/qhull

echo -e "\n\n\033[1;35m###########################################"
echo -e "### Build 3rd Lib start...              ###"
echo -e "###########################################\033[m\n\n"


build_host()
{
  rm CMakeCache.txt

  cmake . -DCMAKE_BUILD_TYPE:STRING=Release \
    -DBUILD_EXAMPLES:BOOL=OFF \
    -DBUILD_PYTHON_BINDINGS:BOOL=OFF \
    -DBUILD_MATLAB_BINDINGS:BOOL=OFF

  make -j${jobs}
  cd ../
}

build_android()
{
  rm CMakeCache.txt

  cmake . -DCMAKE_BUILD_TYPE:STRING=Release \
    -DCMAKE_TOOLCHAIN_FILE:FILEPATH=$ANDROIDTOOLCHAIN \
    -DBUILD_EXAMPLES:BOOL=OFF \
    -DBUILD_PYTHON_BINDINGS:BOOL=OFF \
    -DBUILD_MATLAB_BINDINGS:BOOL=OFF

  make -j${jobs}
  cd ../
}

build_all_host()
{
  cd ${QHULL_ROOT}
  build_host

  cd ${FLANN_ROOT}
  build_host

  cd ${BOOST_ROOT}
  cp ${ROOT}/../CMakeLists.txt ${BOOST_ROOT}
  build_host
}

build_all_android()
{
  cd ${QHULL_ROOT}
  build_android

  cd ${FLANN_ROOT}
  build_android

  cd ${BOOST_ROOT}
  cp ${ROOT}/../CMakeLists.txt ${BOOST_ROOT}
  build_android
}

package_host()
{
  rm -rf ${ROOT}/flann-host
  mkdir -p ${ROOT}/flann-host/lib
  mkdir -p ${ROOT}/flann-host/include

  mv ${ROOT}/flann/lib/* ${ROOT}/flann-host/lib
  cp -rf ${ROOT}/flann/src/cpp/flann ${ROOT}/flann-host/include

  rm -rf ${ROOT}/boost-host
  mkdir -p ${ROOT}/boost-host/lib
  mkdir -p ${ROOT}/boost-host/include

  mv ${ROOT}/boost/libboost*.a ${ROOT}/boost-host/lib/
  cp -rf ${BOOST_ROOT}/boost ${ROOT}/boost-host/include/

  rm -rf ${ROOT}/qhull-host
  mkdir -p ${ROOT}/qhull-host/lib
  #mkdir -p ${ROOT}/qhull-host/include

  mv ${ROOT}/qhull/libqhull*.a ${ROOT}/qhull-host/lib/
  cp -rf ${ROOT}/qhull/src ${ROOT}/qhull-host/include
}

package_android()
{
  rm -rf ${ROOT}/flann-android
  mkdir -p ${ROOT}/flann-android/lib
  mkdir -p ${ROOT}/flann-android/include

  mv ${ROOT}/flann/lib/* ${ROOT}/flann-android/lib
  cp -rf ${ROOT}/flann/src/cpp/flann ${ROOT}/flann-android/include
  rm -rf ${ROOT}/boost-android
  mkdir -p ${ROOT}/boost-android/lib
  mkdir -p ${ROOT}/boost-android/include

  mv ${ROOT}/boost/libboost*.a ${ROOT}/boost-android/lib/
  cp -rf ${BOOST_ROOT}/boost/ ${ROOT}/boost-android/include

  rm -rf ${ROOT}/qhull-android
  mkdir -p ${ROOT}/qhull-android/lib
  #mkdir -p ${ROOT}/qhull-host/include

  mv ${ROOT}/qhull/libqhull*.a ${ROOT}/qhull-android/lib/
  cp -rf ${ROOT}/qhull/src ${ROOT}/qhull-android/include
}
#build_all_android 2>&1 | tee tmp_3rd.log
package_android
